import axios from "axios";
import router from "@/router";

const request = axios.create({
    baseURL:"/api"
})

request.interceptors.request.use(config => {
    let AccessToken = window.sessionStorage.getItem('AccessToken')
    config.headers.Authorization = `Bearer ${AccessToken}`
    console.log('config.headers.Authorization',config.headers.Authorization)
    return config
})

request.interceptors.response.use(resp => {
    return resp
}, async err => {
    if (err.response.status === 401 && router.currentRoute.name !== 'Login') {
        window.sessionStorage.removeItem("AccessToken")
        window.sessionStorage.removeItem("RefreshToken")
        router.replace({
            "name": "Login"
        })
    }
    return Promise.reject(err);
})

export default request